/************** O T T R   C O P Y R I G H T   N O T I C E **************
 **                                                                   **
 ** © 2013 OTTR, Inc.  All rights reserved.                           **
 **                                                                   ** 
 *************** O T T R   C O P Y R I G H T   N O T I C E *************/

package com.jonathan.javatar;

import java.io.Serializable;

/**
 *
 * @author Jonathan Paz <jonathan.paz@ottr.com>
 */
abstract class TarHeader implements Cloneable, Serializable {

    public TarHeader(byte[] block) {

    }

    /**
     * The header block this record represents.
     * @return the header block
     */
    public abstract byte[] getHeaderBlock();

    /**
     * Calculates the header checksum. A class implementing this method may use
     * it to construct the header block.
     */
    abstract void calculateChksum();

    final int firstNullAfter(byte[] block, int offset) {
        for (int i = offset + 1; i < block.length; i++) {
            if (block[i] == 0) {
                return i;
            }
        }
        return block.length;
    }

    final int firstNullAfter(byte[] block, int offset, int length) {
        length = Math.min(length,block.length);
        for (int i = offset + 1; i < length; i++) {
            if (block[i] == 0) {
                return i;
            }
        }
        return length;
    }

    static int firstNullOrSpaceAfter(byte[] block, int offset) {
        for (int i = offset + 1; i < block.length; i++) {
            if (block[i] == 0 || block[i] == ' ') {
                return i;
            }
        }
        return block.length;
    }

    static int firstNullOrSpaceAfter(byte[] block, int offset, int length) {
        length = Math.min(length,block.length);
        for (int i = offset + 1; i < length; i++) {
            if (block[i] == 0 || block[i] == ' ') {
                return i;
            }
        }
        return length;
    }
}
